<?php
namespace Act\Os;
use Lib\ErrorMessage;
use Lib\Common;
class Group extends Backend{
	public function doGet(){
		$group_id = get('group_id',0,'absint');
		$this->checkParam($group_id);
		$user_group_mod = model('#UserGroup','adm_');
		$group = $user_group_mod->getGroupById ( $group_id );
		if(empty($group)){
			return $this->error(ErrorMessage::GROUP_NOT_EXIST,"os/groups");
		}

		#if post...

		$this->display('index.php',array(
			'group'=>$group,
			'user_infos'=>model('#User','adm_')->getUsersByGroup($group_id),
			'groupOptions'=>$user_group_mod->getGroupForOptions(),
		));
	}

	public function doPost(){
		$group_id = get('group_id',0,'absint');
		$this->checkParam($group_id);
		$user_group_mod = model('#UserGroup','adm_');
		$group = $user_group_mod->getGroupById ( $group_id );
		if(empty($group)){
			return $this->error(ErrorMessage::GROUP_NOT_EXIST,"os/groups");
		}
		$method = $user_ids = $user_group = '';
		extract($_POST,EXTR_IF_EXISTS);
		if( empty($user_ids) || empty($user_group)){
			return $this->error(ErrorMessage::NEED_PARAM,'os/groups');
		}else{
			if(in_array(1,$user_ids)){
				$this->error ('不可更改初始管理员的账号组','os/groups');
			}
			$user_ids=implode(',',$user_ids);
			$update_data = array ('user_group' => $user_group);
			$result = model('#User','adm_')->batchUpdateUsers ($user_ids,$update_data );
			
			if ($result>=0) {
				$this->sysLog(session('user_info')['user_name'], 'MODIFY', 'User' ,$user_ids, json_encode($update_data) );
				return $this->success ('更新完成','os/groups');
			} else {
				return $this->error("error",'os/groups');
			}
		}
	}

	public function doAdd(){
		if(is_post()){
			$group_name = $group_desc = '';
			extract ( $_POST, EXTR_IF_EXISTS );
			$user_group_mod = model('#UserGroup','adm_');
			$exist = $user_group_mod->getGroupByName($group_name);
			if($exist){
				$this->alert("error",ErrorMessage::NAME_CONFLICT);
			}else if($group_name ==""){
				
				$this->alert("error",ErrorMessage::NEED_PARAM);
			}else{
				$input_data = array ('group_name' => $group_name, 'group_desc' => $group_desc, 'group_role' => "1,5,17,18,22,23,24,25" ,'owner_id' => session('user_info')['user_id'] );
				$group_id = $user_group_mod->addGroup ( $input_data );
				if ($group_id) {
					model('#SysLog','adm_')->addLog ( session('user_info')['user_name'], 'ADD', 'UserGroup' ,$group_id, json_encode($input_data) );
					return $this->success ('账号组添加完成','os/groups');
				}
			}
		}

		$this->display('from.php');
	}

	public function doUpdate(){
		$group_id = get('group_id',0,'absint');
		$this->checkParam($group_id);
		$user_group_mod = model('#UserGroup','adm_');
		$group = $user_group_mod->getGroupById ( $group_id );

		if(empty($group)){
			return $this->error(ErrorMessage::GROUP_NOT_EXIST,"os/groups");
		}

		if (is_post()) {
			$group_name = $group_desc = '';
			extract ( $_POST, EXTR_IF_EXISTS );
			if($group_name =="" ){
				$this->alert("error",ErrorMessage::NEED_PARAM);
			}else{
				$update_data = array ('group_name' => $group_name, 'group_desc' => $group_desc);
				$result = $user_group_mod->updateGroupInfo ( $group_id,$update_data );
				if ($result>=0) {
					$this->sysLog( session('user_info')['user_name'], 'MODIFY', 'UserGroup' ,$group_id, json_encode($update_data) );
					return $this->success ( '账号组修改完成','os/groups' );
				} else {
					return $this->error("error",'os/groups');
				}
			}
		}

		$this->display('update.php',array(
			'group'=>$group,
			'groupOptions'=>$user_group_mod->getGroupForOptions(),
		));
	}
}